﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.SECURITY.BLL;
//using KPIS.GERP.ESS.BLL;

namespace KPIS.GERP.SECURITY
{
    public partial class Master : System.Web.UI.MasterPage
    {
        private MessageSend.MessageSend send = new MessageSend.MessageSend();
        private SysMenuItemListBLL menuBLL = new SysMenuItemListBLL(); 
        private SsMng ssMng = new SsMng();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                HttpCookie cookie = Request.Cookies["UserDetail"];
                if (cookie != null)
                {
                    Session["EmpSeq"] = cookie["EmpSeq"];
                    Session["UserName"] = cookie["UserName"];
                    Session["UserSeq"] = cookie["UserSeq"];
                    ssMng.SetLoginSession(
                        Convert.ToString(Session["UserSeq"])
                        , Convert.ToString(Session["EmpSeq"])
                        , Convert.ToString(Session["UserName"]));
                }
                //else
                //{
                //    Session["EmpSeq"] = "29";
                //    Session["UserSeq"] = "24";
                //}
                CreatedMenu(ssMng.UserSeq);//Session["UserSeq"].ToString());
            }
            SetPageTitle("ระบบควบคุมสิทธิ์การใช้งาน");
        }

        private void SetPageTitle(string moduleTitle)
        {
            if (!String.IsNullOrEmpty(Page.Title)) return;
            string pageFileName = Path.GetFileName(Request.Path);
            string pageTitle = menuBLL.GetTitlePage(pageFileName);
            pageTitle = String.IsNullOrEmpty(pageTitle) ? moduleTitle :
                String.Format("{0} - {1}", moduleTitle, pageTitle);
            Page.Title = pageTitle;
        }

        protected void UpdateUnreadCount()
        {
            int nUnreadCount = send.GetUnreadCount(ssMng.EmpSeq);//Session["EmpSeq"].ToString());
            int nOldUnreadCount = Convert.ToInt32(Session["UnreadCount"]);

            if (nUnreadCount > nOldUnreadCount)
            {
                ShowNotification(nUnreadCount);
            }

            Session["UnreadCount"] = nUnreadCount;
            lblInboxCount.Text = nUnreadCount.ToString();
        }

        protected void ShowNotification(int nUnreadCount)
        {
            lbNotification.Text = "คุณมีข้อความที่ยังไม่ได้อ่าน " + nUnreadCount.ToString();
            RadToolTip1.Show();
        }

        public void CreatedMenu(string strUserSeq)
        {
            RadMenu1.DataSource = menuBLL.LoadMenuByUser(Convert.ToInt32(strUserSeq));
            RadMenu1.DataBind();
        }

        protected void cmdLogout_Click(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["UserDetail"];

            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddDays(-1);
            }

            Session.Abandon();
            Response.Redirect(@"/ESS/App_UI/Forms/SelectSystem.aspx");
        }

        protected void cmdInbox_Click(object sender, EventArgs e)
        {
            Response.Redirect(@"~\App_UI\Forms\TaskLists.aspx");
        }
    }
}
